package eclipse.util.io;
import java.util.*;
import java.io.*;
import java.net.URL;
import eclipse.*;
/**
 *The DataFile class handles all functions that a binary file might use.
 *The DataFile class also symbolizes the DataFile itself as well as a reader for the file.
 *The use of this class is depricated because the PropertyFile class organizes better and the comCode decoder functions better.  
 */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public final class DataFile 
{
	//Object used to write to the file
	private FileWriter 	pFile;
	//Object used to read the file
	private Scanner 	InputScan;
	/** 
	 * Reads a String from a binary file.
	 * @param sFile Overwrites the current file for a new file 
	 * */
	public String readLine(String sFile)
	{
		//attempt to read a line
		try
		{
			//scan for a single line			
			InputScan = new Scanner(new FileInputStream(new File(sFile)));
			//return the line
			return readLine();
		}
		//An error occurred
		catch(Exception e)
		{
			//handle the error
			ErrorManager.handleError(e);
			//return a null String because there was an error
			return "";
		}					
	}
	/** Reads a String from a binary file */
	public String readLine()
	{
		//attempt to read a line
		try
		{
			return InputScan.nextLine();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return "";
		}
	}
	/** 
	 * Reads a double from a binary file
	 * @param sFile Overwrites the current data file with the specified new file  
	 */
	public double readDbl(String sFile)
	{
		try
		{
			InputScan = new Scanner(new FileInputStream(new File(sFile)));
			return readDbl();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return 0;
		}	
	}
	/** Reads a double from a binary file */
	public double readDbl()
	{
		return InputScan.nextDouble();
	}
	/** 
	 * Reads a float from the current binary file.
	 * @param sFile Overwrites the current binary file with the specified path of another.
	 */
	public float readFlt(String sFile)
	{
		try
		{
			InputScan = new Scanner(new FileInputStream(new File(sFile)));
			return readFlt();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return 0;
		}	
	}
	/** Reads a float from the current binary file */
	public float readFlt()
	{
		return InputScan.nextFloat();
	}
	/** 
	 * Reads a long from the current binary file.
	 * @param sFile Overwrites the current file with the specified new file.
	 */
	public long readLng(String sFile)
	{
		try
		{
			InputScan = new Scanner(new FileInputStream(new File(sFile)));
			return readLng();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return 0;
		}	
	}
	/** Reads a long from the current binary file */
	public long readLng()
	{
		return InputScan.nextLong();
	}
	/**
	 * Reads an integer from the current binary file
	 * @param sFile Overwrites the current binary file with the specified.
	 */
	public int readInt(String sFile)
	{
		try
		{
			InputScan = new Scanner(new FileInputStream(new File(sFile)));
			return readInt();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return 0;
		}	
	}
	/**Reads an integer from the current binary file */
	public int readInt()
	{
		return InputScan.nextInt();
	}
	/**
	 * Reads a short from the current binary file
	 * @param sFile Overwrites the current binary file with a new file.
	 */
	public short readShort(String sFile)
	{
		try
		{
			InputScan = new Scanner(new FileInputStream(new File(sFile)));
			return readShort();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return 0;
		}	
	}
	/**Reads a short from the current binary file */
	public short readShort()
	{
		return InputScan.nextShort();
	}
	/**
	 * Reads a byte from the current binary file.
	 * @param sFile Specifies and loads a new file
	 */
	public byte readByt(String sFile)
	{
		try
		{
			InputScan = new Scanner(new FileInputStream(new File(sFile)));
			return readByte();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return 0;
		}	
	}
	/** Reads a byte from the current binary file. */
	public byte readByte()
	{
		return InputScan.nextByte();
	}
	/** 
	 * Writes a short to the current binary file
	 * @param sFile Sets the current binary file
	 * @param sOutput Short value written to the binary file
	 */
	public void writeShort(String sFile, short sOutput)
	{
		loadFile(sFile);
		writeShort(sOutput);
	}
	/**
	 * Writes a short to the current binary file
	 * @param sOutput Short value written to the binary file
	 */
	public void writeShort(short sOutput)
	{
		writeLine(Short.toString(sOutput));
	}
	/**
	 * Writes a double to the current binary file.
	 * @param sFile String path for the binary file.
	 * @param dOutput Double written to the binary file.
	 */
	public void writeDbl(String sFile, double dOutput)
	{
		loadFile(sFile);
		writeDbl(dOutput);
	}
	/**
	 * Writes a double to the current binary file.
	 * @param dOutput Double written to the binary file.
	 */
	public void writeDbl(double dOutput)
	{
		writeLine(Double.toString(dOutput));
	}
	/** 
	 * Writes a float to the current binary file.
	 * @param sFile String path for the binary file.
	 * @param fOutput Float value written to the binary file.
	 */
	public void writeFlt(String sFile, float fOutput)
	{
		loadFile(sFile);
		writeFlt(fOutput);
	}
	/**
	 * Writes a float to the current binary file.
	 * @param fOutput Float value written to the binary file.
	 */
	public void writeFlt(float fOutput)
	{
		writeLine(Float.toString(fOutput));
	}
	/** 
	 * Writes a long to the current binary file.
	 * @param sFile String path of the binary file.
	 * @param lOutput Long value written to the binary file.
	 */
	public void writeLng(String sFile, long lOutput)
	{
		loadFile(sFile);
		writeLng(lOutput);
	}
	/**
	 * Writes a long to the current binary file.
	 * @param lOutput Long value written to the binary file.
	 */
	public void writeLng(long lOutput)
	{
		writeLine(Long.toString(lOutput));
	}
	/**
	 * Writes an integer to the current binary file.
	 * @param sFile String path of the binary file.
	 * @param iOutput Integer written to the binary file.
	 */
	public void writeInt(String sFile, int iOutput)
	{
		loadFile(sFile);
		writeInt(iOutput);
	}
	/**
	 * Writes an integer to the current binary file.
	 * @param iOutput Integer written to the binary file.
	 */
	public void writeInt(int iOutput)
	{
		writeLine(Integer.toString(iOutput));		
	}
	/**
	 * Writes a byte to the current binary file.
	 * @param sFile String path for the binary file.
	 * @param bOutput Byte written to the binary file.
	 */
	public void writeByte(String sFile, byte bOutput)
	{
		loadFile(sFile);
		writeByte(bOutput);
	}
	/**
	 * Writes a byte to the current binary file.
	 * @param bOutput Byte written to the binary file.
	 */
	public void writeByte(byte bOutput)
	{
		writeLine(Byte.toString(bOutput));
	}
	/**
	 * Writes a String to the current binary file.
	 * @param sFile String path to the binary file.
	 * @param sOutput String written to the binary file.
	 */
	public void writeString(String sFile, String sOutput)
	{
		loadFile(sFile);
		writeString(sOutput);
	}
	/**
	 * Writes a String to the current binary file.
	 * @param sOutput String written to the binary file.
	 */
	public void writeString(String sOutput)
	{
		try
		{
			pFile.write(sOutput);
			pFile.flush();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}
	}
	/**
	 * Writes a line to the binary file.
	 * @param sFile String path to the binary file.
	 * @param sOutput Line written to the binary file.
	 */
	public void writeLine(String sFile, String sOutput)
	{
		loadFile(sFile);
		writeLine(sOutput);
	}
	/**
	 * Writes a line to the binary file.
	 * @param sOutput Line written to the binary file.
	 */
	public void writeLine(String sOutput)
	{
		try
		{
			pFile.write(sOutput + "\n");
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}
	}
	/**
	 * Loads a new binary file.
	 * @param uFile URL path to the binary file.
	 */
	public void loadFile(URL uFile)
	{		
		loadFile(uFile.getFile());
	}
	/**
	 * Loads a new binary file.
	 * @param sFile String path to the binary file.
	 */
	public void loadFile(String sFile)
	{		
		try
		{
			pFile = new FileWriter(sFile);
			InputScan = new Scanner(sFile);
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}
	}
	/**
	 * Closes the binary file.
	 */
	public void closeFile()
	{
		InputScan = null;
		pFile = null;		
	}
	/**
	 * Creates a new DataFile Class with specified Data File path.
	 * @param sFile String path to the DataFile.
	 */
	public DataFile(String sFile)
	{
		loadFile(sFile);
	}
	/**
	 * Creates a new DataFile Class with specified Data File path.
	 * @param uFile URL path to the DataFile.
	 */
	public DataFile(URL uFile)
	{
		loadFile(uFile);
	}
	public void finalize()
	{
		closeFile();
	}
}
